/**
 * Created by zhubuqing on 2018/3/22.
 */
var getBannerUrl = baseUrl + "/common/schoolCommunicate/getBanner";
var getSchoolDynamicsUrl = baseUrl + "/common/schoolCommunicate/getSchoolDynamics";
var getMarxSchoolUrl = baseUrl + "/common/schoolCommunicate/getMarxSchool";
var getNewsUrl = baseUrl + "/common/schoolCommunicate/getNews";
var getSchoolByIdUrl = baseUrl + "/common/schoolCommunicate/getSchoolById";

$(function () {
    // 获取Banner图
    getBanner();

    // 获取各校动态
    getSchoolDynamics("DYNAMICS", "addSchoolDynamics");

    // 获取马克思主义学院
    // getMarxSchool();

    // 浙江新闻获取
    getNews(2, "addNewsZheJiangNews");

    // 专题研究获取
    getSchoolDynamics("RESEARCH", "addZhuanTiYanJiu");

    // 成果展示
    getSchoolDynamics("HARVEST", "addHarvest");

    // 专家观点
    getSchoolDynamics("OPINION", "addOpinion");

    // 会议记录
    getSchoolDynamics("REPORT", "addReport");
})

/**
 * Banner图获取
 */
function getBanner() {
    var data = {
        type: "SCHOOL_COMMUNICATE"
    };
    commonAjax(getBannerUrl, data, "addBanner", "GET");
}

function addBanner(res) {
    var bannerHtml = "";
    for (var i = 0; i < res.obj.length; i++) {
        if (i == 0) {
            bannerHtml += "<div class='item active'><a href='" + res.obj[i].url + "' target='_blank'>";
        } else {
            bannerHtml += "<div class='item'><a href='" + res.obj[i].url + "' target='_blank'>";
        }
        bannerHtml += "<img src='" + res.obj[i].pic + "' alt='...' style='width:100%;height:250px;'>";
        bannerHtml += "<div class='carousel-caption' style='padding-bottom:20px;padding-top:8px;'>";
        bannerHtml += res.obj[i].title;
        bannerHtml += "</div></a></div>";
    }
    $("#banner").html(bannerHtml);

    var bannerPointHtml = "";
    for (var i = 0; i < res.obj.length; i++) {
        if (i == 0) {
            bannerPointHtml += "<li data-target='#carousel-example-generic' data-slide-to='" + 0 + "' class='active'></li>";
        } else {
            bannerPointHtml += "<li data-target='#carousel-example-generic' data-slide-to='" + i + "'></li>";
        }
    }
    $("#bannerPoint").html(bannerPointHtml);
}

/**
 * 各校动态获取
 */
function getSchoolDynamics(type, method) {
    var data = {
        page: 0,
        size: 5,
        type: type
    }
    commonAjax(getSchoolDynamicsUrl, data, method, "GET");
}

function addSchoolDynamics(res) {
    var schoolDynamicsListHtml = "";
    var resultData = res.obj.content;
    for (var i = 0; i < resultData.length; i++) {
        schoolDynamicsListHtml += "<a onclick=toXqNormal('" + resultData[i].id + "','mxjl')><li class='sanleftli'>";
        schoolDynamicsListHtml += "<img src='" + baseUrl + resultData[i].pic + "' alt='' class='img-rounded pull-left'>";
        schoolDynamicsListHtml += "<div class='pull-left sanleft'>";
        schoolDynamicsListHtml += "<p>" + resultData[i].title + "</p>";
        schoolDynamicsListHtml += "<p class='sanleftli-time'>" + changeTime(resultData[i].creatTime) + "</p>";
        schoolDynamicsListHtml += "</div></li></a>";
    }
    $("#schoolDynamicsList").html(schoolDynamicsListHtml);
}

/**
 * 马克思主义学院获取
 */
// function getMarxSchool() {
//     var data = {
//         page: 0,
//         size: 5
//     }
//     commonAjax(getMarxSchoolUrl, data, "addMarxSchool", "GET");
// }
//
// function addMarxSchool(res) {
//     var marxSchoolListHtml = "";
//     var resultData = res.obj.content;
//     for (var i = 0; i < resultData.length; i++) {
//         marxSchoolListHtml += "<a><li onclick=toMarx('" + resultData[i].url + "')>" + resultData[i].marxSchool + "</li></a>";
//     }
//     $("#marxSchoolList").html(marxSchoolListHtml);
// }
//
// function toMarx(url) {
//     window.open(url);
// }

/**
 * 获取新闻
 * @param typeId
 */
function getNews(typeId, method) {
    var data = {
        page: 0,
        size: 5,
        typeId: typeId
    }
    commonAjax(getNewsUrl, data, method, "GET");
}

/**
 * 浙江新闻
 * @param res
 */
function addNewsZheJiangNews(res) {
    var zheJiangNewsHtml = "";
    var resultData = res.obj.content;
    for (var i = 0; i < resultData.length; i++) {
        zheJiangNewsHtml += "<a onclick=toXqNormal('" + resultData[i].id + "','gg')><li class='clearfix'>";
        zheJiangNewsHtml += "<p class='bigTitle'>" + resultData[i].title + "</p>";
        zheJiangNewsHtml += "<div class='time pull-right'>";
        zheJiangNewsHtml += changeTime(resultData[i].creatTime);
        zheJiangNewsHtml += "</div></li></a>";
    }
    $("#zheJiangNews").html(zheJiangNewsHtml);
}

/**
 * 专题研究
 * @param res
 */
function addZhuanTiYanJiu(res) {
    var zhuanTiYanJiuHtml = "";
    var resultData = res.obj.content;
    for (var i = 0; i < resultData.length; i++) {
        zhuanTiYanJiuHtml += "<a onclick=toXqNormal('" + resultData[i].id + "','gxjl_main_zhuanTiYanJiu')><li>";
        zhuanTiYanJiuHtml += "<span>" + resultData[i].title + "</span>";
        zhuanTiYanJiuHtml += "<div class='time pull-right'>";
        zhuanTiYanJiuHtml += changeTime(resultData[i].creatTime);
        zhuanTiYanJiuHtml += "</div></li></a>";
    }
    $("#zhuanTiYanJiuNews").html(zhuanTiYanJiuHtml);
}

/**
 * 成果展示
 * @param res
 */
function addHarvest(res) {
    var chengGuoZhanShiListHtml = "";
    var resultData = res.obj.content;
    for (var i = 0; i < resultData.length; i++) {
        chengGuoZhanShiListHtml += "<a onclick=toXqNormal('" + resultData[i].id + "','gxjl_main_zhuanTiYanJiu')><div class='imgBoxList'>";
        chengGuoZhanShiListHtml += "<img src='" + baseUrl + resultData[i].pic + "' alt=''>";
        chengGuoZhanShiListHtml += "<p title='" + resultData[i].title + "'>" + limitLength(11, resultData[i].title) + "</p>";
        chengGuoZhanShiListHtml += "<p class='time' id='schoolName" + resultData[i].id + "'></p>";
        chengGuoZhanShiListHtml += "</div></a>";

        // 获取高校名
        var getSchoolByIdData = {
            schoolId: resultData[i].schoolId
        };

        commonAjax(getSchoolByIdUrl, getSchoolByIdData, "addSchoolName", "GET", "schoolName" + resultData[i].id);
    }
    $("#chengGuoZhanShiList").html(chengGuoZhanShiListHtml);
}

/**
 * 专家观点
 * @param res
 */
function addOpinion(res) {
    var zhuanJiaGuanDianListHtml = "";
    var resultData = res.obj.content;
    for (var i = 0; i < resultData.length; i++) {
        console.log(resultData);
        zhuanJiaGuanDianListHtml += "<a onclick=toXqNormal('" + resultData[i].id + "','gxjl_main_zhuanTiYanJiu')><li class='clearfix'  style='margin:15px;width:90%'>";
        zhuanJiaGuanDianListHtml += "<img src='" + baseUrl + resultData[i].pic + "' alt='' class='img-rounded pull-left' style='width:24%;height:100px'>";
        zhuanJiaGuanDianListHtml += "<div class='sanleftli1 pull-left' style='padding-left:20px;width:75%'>";
        zhuanJiaGuanDianListHtml += "<p style='font-weight:bold'>" + resultData[i].title + "</p><p style='color:#999'>" + resultData[i].brief + "</p>";
        zhuanJiaGuanDianListHtml += "<p class='time' style='text-align:right;margin-top:20px'>" + changeTime(resultData[i].creatTime) + "</p>";
        zhuanJiaGuanDianListHtml += "</div></li></a>";
    }
    $("#zhuanJiaGuanDianList").html(zhuanJiaGuanDianListHtml);
}

/**s
 * 会议记录
 * @param res
 */
function addReport(res) {
    var huiYiJiLuListHtml = "";
    var resultData = res.obj.content;
    for (var i = 0; i < resultData.length; i++) {
        huiYiJiLuListHtml += "<a onclick=toXqNormal('" + resultData[i].id + "','gxjl_main_zhuanTiYanJiu')><li class='clearfix'  style='margin:15px;width:90%'>";
        huiYiJiLuListHtml += "<div class='pull-left sanleftli1' style='padding-left:20px;width:100%'>";
        huiYiJiLuListHtml += "<p style='font-weight:bold'>" + resultData[i].title + "</p><p style='color:#999;'>" + resultData[i].brief + "</p>";
        huiYiJiLuListHtml += "<p class='time' style='text-align:right;margin-top:20px'>" + changeTime(resultData[i].creatTime) + "</p>";
        huiYiJiLuListHtml += "</div></li></a>";
    }
    $("#huiYiJiLuList").html(huiYiJiLuListHtml);
}

function addSchoolName(res, param) {
    var resultData = res.obj;
    $("#" + param).html(resultData.schoolName);
}

/********************************************** Util **********************************************/

// 限制字符串长度
function limitLength(wantLength, str) {
    var returnStr = "";
    var realLength = 0, len = str.length, charCode = -1;
    for (var i = 0; i < len; i++) {
        if (realLength < wantLength) {
            charCode = str.charCodeAt(i);
            if (charCode >= 0 && charCode <= 128)
                realLength += 1;
            else
                realLength += 2;
            returnStr += str.charAt(i);
        } else {
            returnStr += "...";
            break;
        }
    }
    return returnStr;
}

// 跳转至详情
function toXqNormal(followId, type) {
    var followId = followId;
    var theType = type;
    sessionStorage.setItem("followId", JSON.stringify(followId));
    sessionStorage.setItem("type", JSON.stringify(theType));
    self.location.href = "xq.html";
}

// 跳转至视频
function toXqVideo(followId, type) {
    var followId = followId;
    var theType = type;
    sessionStorage.setItem("followId", JSON.stringify(followId));
    sessionStorage.setItem("type", JSON.stringify(theType));
    window.open("video.html");
}